/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.groupjjk.spectatorsportsonline.models.NBA;

import JPA.JPAEntityManager;
import com.groupjjk.spectatorsportsonline.fathers.IModelable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;



/**
 *
 * @author Jrdevelopment
 */
public class ModelNBAGame implements IModelable {

    EntityManager em = JPAEntityManager.getEntityManager();

    @Override
    public boolean insert(Object o) {
        try {
            //DownCasting
         
            em.getTransaction().begin();
            em.persist((NBAGame)o);
            em.flush();
            em.getTransaction().commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();

            return false;
        }

    }

    @Override
    public boolean update(Object o) {
        try {
            //DownCasting
         
            em.getTransaction().begin();
            em.merge((NBAGame)o);
            em.flush();
            em.getTransaction().commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();

            return false;
        }
    }

    @Override
    public boolean delete(Object o) {
        try {
            QuarterType q = (QuarterType) o;
            em.getTransaction().begin();
            em.remove((NBAGame) o);
            em.flush();
            em.getTransaction().commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();

            return false;
        }
    }

    @Override
     public ArrayList<NBAGame> read() throws Exception {

        Query q = em.createQuery("SELECT q FROM NBAGame q");
        List<NBAGame> list = q.getResultList();

        return (ArrayList) list;
    }
}
